package com.xuetang9.t9.hotel.services.impl;

import com.xuetang9.t9.hotel.beans.Arrive;
import com.xuetang9.t9.hotel.beans.BillDetail;
import com.xuetang9.t9.hotel.beans.RoomInfo;
import com.xuetang9.t9.hotel.mapper.IArriveMapper;
import com.xuetang9.t9.hotel.mapper.IBillDetailMapper;
import com.xuetang9.t9.hotel.mapper.ICheckTypeMapper;
import com.xuetang9.t9.hotel.mapper.IRoomInfoMapper;
import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.ArriveCondition;
import com.xuetang9.t9.hotel.services.IArriveService;
import com.xuetang9.t9.hotel.services.IBillDetailService;
import com.xuetang9.t9.hotel.services.IGuestService;
import com.xuetang9.t9.hotel.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 入住业务
 */
@Service
public class ArriveServiceImpl implements IArriveService {

    @Autowired
    private IArriveMapper arriveMapper;

    @Autowired
    private IGuestService guestService;

    @Autowired
    private IBillDetailService billDetailService;

    @Autowired
    private IRoomInfoMapper roomInfoMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Autowired
    private ICheckTypeMapper checkTypeMapper;




    @Override
    public ResponseResult save(Arrive arrive) {
        if(null == arrive) {return null;}
        ResponseResult guestResult = guestService.save(arrive.getGuest());
        if(!guestResult.getCode().equals(HttpCodeEnum.SUCCESS.getCode())){
            return null;
        }
        Long guestId =  (Long) guestResult.getData();
        arrive.setGuestId(guestId);
        arrive.setArriveId(snowFlake.nextId());
        arriveMapper.insert(arrive);
        //修改房间状态
        RoomInfo roomInfo =roomInfoMapper.selectByRoomId(arrive.getRoomId());
        roomInfo.setStateId(OverallConstant.ROOM_STATE_CHECK);
        roomInfoMapper.update(roomInfo);
        //创建账单明细记录
        BillDetail billDetail = new BillDetail();
        billDetail.setBillId(snowFlake.nextId());
        billDetail.setArriveId(arrive.getArriveId());
        billDetail.setBillOriginalprice(roomInfo.getRoomType().getTypePrice());
        billDetail.setBillDeposit(arrive.getBillDetail().getBillDeposit());
        billDetailService.save(billDetail);
        return null;
    }

    @Override
    public ResponseResult remove(Long id) {
        if(null == id) {return null;}
        billDetailService.removeByArriveId(id);
        arriveMapper.delete(id);
        return null;
    }

    @Override
    public ResponseResult alter(Arrive arrive) {
        if(null == arrive) {
            return null;
        }
        arriveMapper.update(arrive);
        return null;
    }

    @Override
    public PageBean queryByAll(ICondition condition) {
        if(condition == null){
            condition = new ArriveCondition();
        }
        List<Arrive> arrives = arriveMapper.selectByAll(condition);
        PageBean pageBean = new PageBean();
        if(null != arrives && arrives.size() != 0){
            Long count = queryByCount(condition);
            pageBean.setCount(count);
            pageBean.ok(arrives);
            return pageBean;
        }
        pageBean.error(HttpCodeEnum.DATA_NOT_EXIST.getCode(),HttpCodeEnum.DATA_NOT_EXIST.getErrorMessage());
        return pageBean;
    }

    @Override
    public Long queryByCount(ICondition condition) {
        return arriveMapper.selectByCount(condition);
    }

    @Override
    public boolean updateByCheckType(Integer checkTypeId) {
        if(checkTypeId == null){
            return false;
        }
        return arriveMapper.updateByCheckType(checkTypeId) > 0;
    }
}
